package com.gym.baselib.utils

import CountdownTimer
import android.annotation.SuppressLint
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.widget.TextView
import android.widget.Toast
import com.xgst.baselib.BaseApplication
import com.xgst.lib.R

/**
 *创建人：东
 *时间：2021/1/28
 */
@SuppressLint("MissingInflatedId")
object ProgressUtils {

    private var toast: Toast? = null
    private val view: View
    private val tvProgress: TextView
    private var countdownTimer:CountdownTimer ? = null
    init {
        view =
            LayoutInflater.from(BaseApplication.sContext()).inflate(R.layout.progress_layout, null)
        tvProgress = view.findViewById(R.id.tvProgress)
    }

    fun showProgress() {
        if(countdownTimer?.isRunning() == true){
            countdownTimer?.stop()
        }
        show()
        countdownTimer = CountdownTimer(
            initialSeconds = 3,
            onFinish = {
                showProgress()
            })
        countdownTimer!!.start()
    }

    private fun show(content: String = "加载中...") {
        toast?.cancel()
         toast = Toast(BaseApplication.sContext())
        toast!!.let {
            it.setGravity(Gravity.CENTER, 0, 0)
            it.duration = Toast.LENGTH_LONG
            it.view = view
            tvProgress.text = content
            it.show()
        }
    }

    fun dismiss() {
        countdownTimer?.stop()
        toast?.cancel()
    }
}

